﻿using CoreShop.Library;
using CoreShop.Model;
using CoreShop.PrintService;
using CoreShop.Services;
using SqlSugar;
using Sunny.UI;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace CoreShop.App
{
    public partial class frmFoodprintScheme : UIForm
    {
        public frmFoodprintScheme()
        {
            InitializeComponent();
            //uiTreeView1.ExpandAll();
        }
        long foodId;
        private void uiButton1_Click(object sender, System.EventArgs e)
        {
            uiButton1.Selected = false;
            uiButton2.Selected = false;
            uiButton3.Selected = false;
            UIButton btn = (UIButton)sender;
            btn.Selected = true;
        }

        private void LoadFood(string foodName)
        {
            uiTreeView1.Nodes.Clear();

            List<corecmsfood> foodTypeIdList = new corecmsfoodServices().AsQueryable().Where(p => p.name.Contains(foodName)).ToList();
            if (foodTypeIdList.Count > 0)
            {
                var listIds = foodTypeIdList.Select(p => p.foodTypeId);
                var lstSource = new corecmsfoodcategoryServices().QueryAsync(0, "").Where(p => listIds.Contains(p.id));
                foreach (var item in lstSource)
                {
                    TreeNode treeNode10 = new TreeNode(item.name);
                    treeNode10.Tag = item.id.ToString();
                    uiTreeView1.Nodes.Add(treeNode10);
                    List<corecmsfood> lstfood = new corecmsfoodServices().QueryAsync(item.id, foodName);
                    foreach (var items in lstfood)
                    {
                        TreeNode treeNode1 = new TreeNode(items.name);
                        treeNode1.Tag = items.id.ToString();
                        treeNode10.Nodes.Add(treeNode1);
                    }
                }
                if (uiTreeView1.Nodes.Count > 0) uiTreeView1.Nodes[0].Expand();
            }
        }

        private void FDishPinterSetup_Shown(object sender, System.EventArgs e)
        {
            GetSchemeDetail(0, "", 0, 0, 0);
        }

        private void uifDishPinterSetupItemHeader1_CheckBoxChecked(object sender, System.EventArgs e)
        {
            var items = layPrinterList.GetControls<UIDishPinterSetupItem>(true);
            foreach (var item in items)
            {
                item.Checked = uifDishPinterSetupItemHeader1.Checked;
            }
        }

        private void frmFoodprintScheme_Load(object sender, System.EventArgs e)
        {
            LoadFood("");
        }
        private void GetSchemeDetail(long foodId, string printerName, int printerType, int brandId, int sendPrintWay)
        {
            try
            {
                List<corecmsfoodprintscheme> foodCheme = null;
                if (foodId > 0)
                {
                    foodCheme = new corecmsfoodprintschemeServices().QueryAsync(foodId);
                }
                var cheme = new corecmsprintschemeServices().SelectByName(printerName, printerType, brandId, sendPrintWay);
                layPrinterList.Visible = false;
                layPrinterList.Clear();
                foreach (var info in cheme)
                {
                    info.Selected = false;
                    if (foodCheme != null)
                    {
                        var foodChemes = foodCheme.Find(p => p.printSchemeId == info.id);
                        if (foodChemes != null)
                        {
                            info.Selected = true;
                        }
                    }
                    UIDishPinterSetupItem item = new UIDishPinterSetupItem(info);
                    item.CheckBoxChecked += Detail_OnCheckBoxChecked;
                    layPrinterList.Add(item);
                }
                layPrinterList.Visible = true;
            }
            catch (Exception)
            {
                throw;
            }
        }

        private void Detail_OnCheckBoxChecked(object sender, System.EventArgs e)
        {
            if (foodId <= 0) return;
            corecmsprintscheme item = (corecmsprintscheme)sender;
            var foodcheme = new corecmsfoodprintschemeServices().AsQueryable()
               .Where(p => p.printSchemeId == item.id && p.foodId == foodId).First();
            if (foodcheme != null)
            {
                //已存在则不需要做处理
                if (item.Selected == false)
                {
                    new corecmsfoodprintschemeServices().DeleteById(foodcheme.id);
                }
            }
            else
            {
                corecmsfoodprintscheme addfoodcheme = new corecmsfoodprintscheme();
                addfoodcheme.foodId = foodId;
                addfoodcheme.foodSpecId = 0;
                addfoodcheme.printSchemeId = item.id;
                var bl = new corecmsfoodprintschemeServices().Insert(addfoodcheme);
                if (!bl) UIMessageBox.Show("保存失败", "出错:", UIStyle.Orange, UIMessageBoxButtons.OK);
            }
        }

        private void uiTreeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            foodId = CommFunc.ConvertToInt64(e.Node.Tag);
            GetSchemeDetail(foodId, "", 0, 0, 0);
        }

        private void uiButton12_Click(object sender, EventArgs e)
        {
            LoadFood(this.foodName.Text);
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}